package com.catersystem.zmy.catersystem.fragment;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.catersystem.zmy.catersystem.R;
import com.catersystem.zmy.catersystem.activity.HomeActivity;
import com.catersystem.zmy.catersystem.adapter.BehindCookAdapter;
import com.catersystem.zmy.catersystem.base.BaseFragment;
import com.catersystem.zmy.catersystem.bean.DishTypeBean;
import com.catersystem.zmy.catersystem.bean.LatelyCompleteBean;
import com.catersystem.zmy.catersystem.finals.FinalUrl;
import com.catersystem.zmy.catersystem.finals.TimeConstants;
import com.catersystem.zmy.catersystem.httputils.GetDataUtils;
import com.catersystem.zmy.catersystem.httputils.HttpUtils;
import com.catersystem.zmy.catersystem.utils.AudioUtils;
import com.catersystem.zmy.catersystem.utils.DateUtil;
import com.catersystem.zmy.catersystem.utils.TimeUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Timer;
import java.util.TimerTask;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * 按菜分类显示
 * Created by Administrator on 2017/12/4 0004.
 */

public class DishesFragment extends BaseFragment {

    private static final String TAG = "DishesFragment";
    @BindView(R.id.rv_dishes)
    RecyclerView mRvDishes;

    public BehindCookAdapter mBehindCookAdapter; //后厨适配器
    public List<DishTypeBean> mDishTypeBeans = new ArrayList<>();
    private Timer mTimer;
    private TimerTask mTimerTask;

    private HomeActivity mHomeActivity;

    @Override
    protected View initContentView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle
            savedInstanceState) {
        return inflater.inflate(R.layout.fragment_dishes, container, false);
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        this.mHomeActivity = (HomeActivity) context;
    }

    @Override
    protected void initView(View view) {
        mRvDishes.setLayoutManager(new GridLayoutManager(mHomeActivity, 5));
        mBehindCookAdapter = new BehindCookAdapter(mDishTypeBeans);
        mRvDishes.setAdapter(mBehindCookAdapter);
        mBehindCookAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(final BaseQuickAdapter adapter, View view, final int position) {
                Map<String, String> data = new HashMap<String, String>();
                switch (((DishTypeBean) adapter.getData().get(position)).getStatus()) {
                    case 0://未烹饪
                        data.put("orgcode", FinalUrl.getOrgCode());
                        data.put("detailid", ((DishTypeBean) adapter.getData().get(position)).getId() + "");
                        data.put("status", "1");
                        GetDataUtils.getInstance().refreshCookingStatus(data, mHomeActivity, new HttpUtils
                                .IHttpResult() {
                            @Override
                            public void onSuccess(String result) {
                                ((DishTypeBean) adapter.getData().get(position)).setStatus(1);
                                adapter.notifyItemChanged(position);
                                showShortToast("开始烹饪...");
                                mHomeActivity.cookingNum++;
                                mHomeActivity.noCookingNum--;
                                mHomeActivity.tvCooking.setText("正在烹饪　" + mHomeActivity.cookingNum + "");
                                mHomeActivity.tvNoCooking.setText("未烹饪　" + mHomeActivity.noCookingNum + "");
                            }
                        });
                        break;
                    case 1://正在烹饪
                        data.put("orgcode", FinalUrl.getOrgCode());
                        data.put("detailid", ((DishTypeBean) adapter.getData().get(position)).getId() + "");
                        data.put("status", "2");
                        GetDataUtils.getInstance().refreshCookingStatus(data, mHomeActivity, new HttpUtils
                                .IHttpResult() {
                            @Override
                            public void onSuccess(String result) {
                                AudioUtils.getInstance().speakText(((DishTypeBean) adapter.getData().get(position))
                                        .getId() + "号请到后厨取餐！");
                                mDishTypeBeans.remove(((DishTypeBean) adapter.getData().get(position)));
                                adapter.notifyItemRemoved(position);
                                showShortToast("已完成烹饪！");
                                mHomeActivity.cookingNum--;
                                mHomeActivity.tvCooking.setText("正在烹饪　" + mHomeActivity.cookingNum + "");
                            }
                        });

                        break;
                }
            }
        });
        setTimer();
    }

    /**
     * 设置计时器
     */
    private void setTimer() {
        mTimer = new Timer();
        mTimerTask = new TimerTask() {
            @Override
            public void run() {
                mHomeActivity.runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        setDishesWaitTime();
                    }
                });
            }
        };
        mTimer.schedule(mTimerTask, 10000, 10000);
    }

    public void setDishesWaitTime() {
        for (int i = 0; i < mDishTypeBeans.size(); i++) {
            long opertime = (System.currentTimeMillis() / 1000) - mDishTypeBeans.get(i).getOpertime();
            mDishTypeBeans.get(i).setTime(DateUtil.getCutDown1(opertime));
            mBehindCookAdapter.notifyDataSetChanged();
        }
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        if (mTimer != null) {
            mTimer.cancel();
            mTimer = null;
        }
    }
}
